//index.js
//获取应用实例
const app = getApp();
const util = require('../../../utils/util.js')
Page({
  data: {
    items0: [], //全部订单
    pageNum0: 0,
    has0: true,
    loading0: false,

    items1: [], // 待出票订单
    pageNum1: 0,
    has1: true,
    loading1: false,

    items2: [], // 已出票订单
    pageNum2: 0,  
    has2: true,
    loading2: false,

    items3: [], // 已取消订单
    pageNum3: 0,  
    has3: true,
    loading3: false,

    limit: 10,

    swiperViewHeight: 0,
    currentTab: 0,
    inputShowed: false,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1200,
  },
  swiperchange: function (e) {
    //FIXME: 当前页码
    //console.log(e.detail.current)
  },

  onLoad: function (parms) {
    if(JSON.stringify(parms) != '{}') {
      this.setData({
        currentTab: parms.currentTab
      })
    } 
    //调用应用实例的方法获取全局数据
    if(parms.currentTab == 0){
      this.initGet();
    }
  },
  get0: function (type) {
    // 全部订单
    wx.showLoading()
    var that = this;
    if (that.data.loading1) return

    that.data.loading0 = true;
    var pageNum0 = that.data.pageNum0;
    var limit = that.data.limit;
    //allOrderList
    wx.request({
      url: app.globalData.url + '/appletSupplierRob/standbyTicket/7',
      method: 'POST',
      data: {
        pageNum: ++pageNum0,
        pageSize: limit
      },
      header: {
        'Accept': 'application/json',
        consumerId: app.globalData.userInfo.consumerId
      },
      success: function (res) {
        wx.hideLoading();
        if(type == 'pull') {
          wx.stopPullDownRefresh()
        }
        const items0 = that.data.items0.concat(res.data.data.rows && res.data.data.rows.map(i=> {
          return {
            ...i,
            status: that.typeConvert(i.orderBidStatus, 'color'),
            statusName: that.typeConvert(i.orderBidStatus, 'name'),
            totalBidPrice: (i.bidPrice * i.ticketNum).toFixed(2)
          }
        }));
        that.setData({
          has0: pageNum0 <= Math.ceil(res.data.data.total / 10),
          items0,
          pageNum0: pageNum0
        })
        if (that.data.currentTab == 0) {
          that.setSwiperHeight();
        }
        that.data.loading0 = false;
      }
    })
  },
  get1: function (type) {
    // 待出票订单
    wx.showLoading()
    var that = this;
    if (that.data.loading1) return

    that.data.loading1 = true;
    var pageNum1 = that.data.pageNum1;
    var limit = that.data.limit;
    //waitingIssueList
    wx.request({
      url: app.globalData.url + '/appletSupplierRob/standbyTicket/3',
      method: 'POST',
      data: {
        pageNum: ++pageNum1,
        pageSize: limit
      },
      header: {
        'Accept': 'application/json',
        'ConsumerId': app.globalData.userInfo.consumerId
      },
      success: function (res) {
        wx.hideLoading();
        if(type == 'pull') {
          wx.stopPullDownRefresh()
        }
        const items1 = that.data.items1.concat(res.data.data.rows && res.data.data.rows.map(i=> {
          return {
            ...i,
            status: that.typeConvert(i.orderBidStatus, 'color'),
            statusName: that.typeConvert(i.orderBidStatus, 'name'),
            totalBidPrice: (i.bidPrice * i.ticketNum).toFixed(2)
          }
        }));
        that.setData({
          has1: pageNum1 <= Math.ceil(res.data.data.total / 10),
          items1,
          pageNum1: pageNum1
        })
        if (that.data.currentTab == 1) {
          that.setSwiperHeight();
        }
        that.data.loading1 = false;
      }
    })
  },
  get2: function (type) {
    // 已出票订单
    wx.showLoading()
    var that = this;
    if (that.data.loading2) return
    that.data.loading2 = true;
    var pageNum2 = that.data.pageNum2;
    var limit = that.data.limit;
    wx.request({
      url: app.globalData.url + '/appletSupplierRob/standbyTicket/4',
      method: 'POST',
      header:{
        consumerId: app.globalData.userInfo.consumerId
      },
      data: {
        pageNum: ++pageNum2,
        pageSize: limit
      },
      success: function (res) {
        wx.hideLoading();
        if(type == 'pull') {
          wx.stopPullDownRefresh()
        }
        const items2 = that.data.items2.concat(res.data.data.rows && res.data.data.rows.map(i=> {
          return {
            ...i,
            status: that.typeConvert(i.orderBidStatus, 'color'),
            statusName: that.typeConvert(i.orderBidStatus, 'name'),
            totalBidPrice: (i.bidPrice * i.ticketNum).toFixed(2)
          }
        }));
        that.setData({
          has2: pageNum2 <= Math.ceil(res.data.data.total / 10),
          items2,
          pageNum2: pageNum2
        })
        if (that.data.currentTab == 2) {
          that.setSwiperHeight();
        }
        that.data.loading2 = false;
      }
    })
  },
  get3: function (type) {
    // 已取消订单
    wx.showLoading()
    var that = this;
    if (that.data.loading3) return
    that.data.loading3 = true;
    var pageNum3 = that.data.pageNum3;
    var limit = that.data.limit;
    wx.request({
      url: app.globalData.url + '/appletSupplierRob/standbyTicket/5',
      method: 'POST',
      header:{
        consumerId: app.globalData.userInfo.consumerId
      },
      data: {
        pageNum: ++pageNum3,
        pageSize: limit
      },
      success: function (res) {
        wx.hideLoading();
        if(type == 'pull') {
          wx.stopPullDownRefresh()
        }
        const items3 = that.data.items3.concat(res.data.data.rows && res.data.data.rows.map(i=> {
          return {
            ...i,
            status: that.typeConvert(i.orderBidStatus, 'color'),
            statusName: that.typeConvert(i.orderBidStatus, 'name'),
            totalBidPrice: (i.bidPrice * i.ticketNum).toFixed(2)
          }
        }));
        that.setData({
          has3: pageNum3 <= Math.ceil(res.data.data.total / 10),
          items3,
          pageNum3: pageNum3
        })
        if (that.data.currentTab == 3) {
          that.setSwiperHeight();
        }
        that.data.loading3 = false;
      }
    })
  },
  onPullDownRefresh: function(){
    this.initGet('pull')
  },
  onReachBottom: function () {
    if (this.data.currentTab == 0 && this.data.has0)
      this.get0();
    else if (this.data.currentTab == 1 && this.data.has1)
      this.get1();
    else if (this.data.currentTab == 2 && this.data.has2)
      this.get2();
    else if (this.data.currentTab == 3 && this.data.has3)
      this.get3();
  },
  onReachBottom: function name(params) {
    this.initGet()
  },
  setSwiperHeight: function () {
    var query = wx.createSelectorQuery();
    //选择id
    var that = this;
    var px1 = 208 / 750 * wx.getSystemInfoSync().windowWidth;
    var px2 = 376 / 750 * wx.getSystemInfoSync().windowWidth;
    query.selectAll('.swiperH').boundingClientRect(function (rect) {
      // console.log(rect[0].height)
      // var itemsLength = 0;
      // itemsLength = rect[0].height * that.data.itemsShow.length - 12;

      var itemsLength = 0;
      if (that.data.currentTab == 0){
        itemsLength = (178 + 18)*that.data.items0.length + 18;
      }else if(that.data.currentTab == 1){
        itemsLength = (178 + 18)*that.data.items1.length + 18;
      }else if(that.data.currentTab == 2){
        itemsLength = (178 + 18)*that.data.items2.length + 18;
      } else if(that.data.currentTab == 3){
        itemsLength = (178 + 18)*that.data.items3.length + 18;
      }
      that.setData({
        swiperViewHeight: itemsLength
      })
    }).exec();
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    },()=> {
      that.initGet()
      wx.pageScrollTo({
        scrollTop: 0
      })
    });
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  typeConvert: function(val, name){
    let status = {
      0:'初始化状态',
      1:'竞价中',
      2:'选单中',
      3:'待出票',
      4:'已出票',
      5:'已退款',
      6:'已完成',
      7:'全部'
    }
    let color = {
      0: 'color0',
      1: 'color1',
      2: 'color1',
      3: 'color2',
      4: 'color1',
      5: 'color2',
    }
    if (name == 'name') {
      return status[val]
    } else if (name == 'color') {
      return color[val]
    }
  },
  submit(e) {
    console.log(e.currentTarget);
    const item = e.currentTarget.dataset.item
      const paramsStr = JSON.stringify(item);
      wx.navigateTo({
        url: `/pages/subPages/snack-detail/snack-detail?paramsStr=${encodeURIComponent(paramsStr)}`,
      })
  },
  initGet(type){
    if (this.data.currentTab == 0) {
      this.setData({
        items0: [],
        pageNum0: 0,
        has0: true,
        loading0: false,
      },()=> {
        this.get0(type);
      })
    } else if (this.data.currentTab == 1) {
    this.setData({
      items1: [],
      pageNum1: 0,
      has1: true,
      loading1: false,
    }, ()=> {
      this.get1(type);
    })
  } else if (this.data.currentTab == 2) {
      this.setData({
        items2: [],
        pageNum2: 0,  
        has2: true,
        loading2: false,
      }, ()=> {
        this.get2(type);
      })
    } else if (this.data.currentTab == 3) {
      this.setData({
        items3: [],
        pageNum3: 0,  
        has3: true,
        loading3: false,
      }, ()=> {
        this.get3(type);
      })
    }
  }
})